{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "49e3a7b4",
   "metadata": {},
   "source": [
    "\n",
    "# 如何在vscode用jupyter notebook运行和调试Python代码\n",
    "\n",
    "1. 安装VSCode和Python扩展\n",
    "   - 下载并安装Visual Studio Code（VSCode）。\n",
    "   - 打开VSCode，进入扩展市场，搜索并安装“Python”扩展。\n",
    "   - 确保已安装Python解释器，并在VSCode中正确配置Python路径。\n",
    "2. 安装Jupyter扩展\n",
    "   - 在VSCode的扩展市场中，搜索并安装“Jupyter”扩展。    \n",
    "   - 该扩展允许你在VSCode中创建和运行Jupyter Notebook。\n",
    "3. 创建Jupyter Notebook\n",
    "   - 在VSCode中，按下`Ctrl+Shift+P`（Windows/Linux）或`Cmd+Shift+P`（Mac）打开命令面板。\n",
    "   - 输入“Jupyter: Create New Jupyter Notebook”并选择该选项。\n",
    "   - 选择保存位置和文件名，创建一个新的`.ipynb`文件。\n",
    "   - 创建虚拟环境（可选）\n",
    "     - 切换到当前项目目录：\n",
    "       ```bash\n",
    "       cd /path/to/your/project\n",
    "       ```\n",
    "     - 在终端中运行以下命令创建虚拟环境：\n",
    "       ```bash\n",
    "       python -m venv myenv\n",
    "       ```\n",
    "     - 激活虚拟环境：\n",
    "       - Windows:\n",
    "         ```bash\n",
    "         myenv\\Scripts\\activate\n",
    "         ```\n",
    "       - Mac/Linux:\n",
    "         ```bash\n",
    "         source myenv/bin/activate\n",
    "         ```\n",
    "     - 安装Jupyter：\n",
    "       ```bash\n",
    "       pip install jupyter\n",
    "       ```\n",
    "4. 编写和运行代码\n",
    "   - 在新创建的Jupyter Notebook中，你可以添加代码单元格。\n",
    "   - 输入Python代码后，点击单元格左侧的运行按钮，或者按下`Shift+Enter`运行代码。\n",
    "   - 运行结果会显示在   单元格下方。\n",
    "5. 调试代码\n",
    "   - 在代码单元格中设置断点：点击代码行号左侧的空白区域，添加断点。\n",
    "   - 右键点击代码单元格，选择“Debug Cell”选项，启动调试模式。\n",
    "   - 使用调试工具栏中的按钮（如继续、单步执行等）来控制调试过程。\n",
    "   - 你可以查看变量值、调用堆栈等调试信息。\n",
    "6. 保存和导出Notebook\n",
    "   - 完成代码编写和调试后，按下`Ctrl+S`（Windows/Linux）或`Cmd+S`（Mac）保存Notebook。\n",
    "   - 你还可以通过命令面板选择“Jupyter: Export to HTML”或其他格式导出Notebook。\n",
    "通过以上步骤，你就可以在VSCode中使用Jupyter Notebook运行和调试Python代码了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5586bc07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello, World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0d8d4ad",
   "metadata": {},
   "source": [
    "学习数据分析，并不需要非常深入的编程基础知识，因为大部分操作都是通过第三方库来完成的。掌握一些常用的Python语法和数据分析库的使用就可以了。先学会做一个合格的调包侠!<br>\n",
    "后面的内容会假设同学们掌握了python基本语法的基础上，比如，数据类型，循环，条件判断等的基础上，特别着重介绍一些<u>数据分析</u>中常用的语法。<br>\n",
    "如果你是一个完全Python零基础的新手，先不要慌张，建议先去B站或者YouTube上找一些Python入门教程，先把基础语法学会，然后掌握如何安装Python、配置虚拟环境和安装第三方库。花点时间，一周包会的。<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16f8037c",
   "metadata": {},
   "source": [
    "### 列表推导式\n",
    "列表推导式是Python中一种简洁的创建列表的方式。它可以通过对现有列表的操作，快速生成新的列表。\n",
    "\n",
    "#### 基本语法\n",
    "```python\n",
    "new_list = [expression for item in iterable if condition]\n",
    "```\n",
    "- `expression`：对每个元素进行的操作。\n",
    "- `item`：当前遍历的元素。\n",
    "- `iterable`：可迭代对象，如列表、元组等。\n",
    "- `condition`：可选的过滤条件。\n",
    "\n",
    "#### 示例\n",
    "1. 生成平方数列表\n",
    "```python\n",
    "squares = [x**2 for x in range(10)]\n",
    "```\n",
    "2. 过滤偶数\n",
    "```python\n",
    "evens = [x for x in range(10) if x % 2 == 0]\n",
    "```\n",
    "3. 结合字符串操作\n",
    "```python\n",
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "fruit_lengths = [len(fruit) for fruit in fruits]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0605dfda",
   "metadata": {},
   "source": [
    "##### 例题1： 统计句子中每个单词的长度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6d5456cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Data': 4, 'analysis': 8, 'with': 4, 'Python': 6, 'is': 2, 'fun': 3}\n"
     ]
    }
   ],
   "source": [
    "sentence = \"Data analysis with Python is fun\"\n",
    "word_lengths = {word: len(word) for word in sentence.split()}\n",
    "print(word_lengths)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a10373c0",
   "metadata": {},
   "source": [
    "### 字典推导式 & 集合推导式\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "068a2c8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典：键为字母，值为索引\n",
    "letters = ['a', 'b', 'c']\n",
    "d = {v: i for i, v in enumerate(letters)}  \n",
    "print(d)\n",
    "\n",
    "# 集合：去重\n",
    "unique = {x for x in [1,2,2,3]}  \n",
    "print(unique)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63d022fb",
   "metadata": {},
   "source": [
    "### 常用内置函数与语法（数据分析中高频使用）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0993f28",
   "metadata": {},
   "source": [
    "#### len()\n",
    "`len()`函数用于返回对象的长度（元素个数）。\n",
    "```python\n",
    "my_list = [1, 2, 3, 4]\n",
    "length = len(my_list)  # 输出: 4\n",
    "```\n",
    "二维数组的长度，即行数\n",
    "```python\n",
    "matrix = [[1, 2], [3, 4, 5], [6, 7, 8]]\n",
    "rows = len(matrix)  # 输出: 3\n",
    "每一行的列数\n",
    "cols = [len(row) for row in matrix]  # 输出: [2, 3, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "736c3bcf",
   "metadata": {},
   "source": [
    "### type()\n",
    "查看数据类型\n",
    "```python\n",
    "d = [1, 2, 3]\n",
    "print(type(d))  # 输出: <class 'list'>\n",
    "e = (1, 2, 3)\n",
    "print(type(e))  # 输出: <class 'tuple'>\n",
    "f = {'a': 1, 'b': 2}\n",
    "print(type(f))  # 输出: <class 'dict'>\n",
    "matrix = [[1, 2], [3, 4]]\n",
    "print(type(matrix))  # 输出: <class 'list'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "77474f03-c96e-46b5-8cff-4a0ccb005909",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "f = {'a': 1, 'b': 2}\n",
    "print(type(f)) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87870174",
   "metadata": {},
   "source": [
    "### zip()\n",
    "`zip()`函数用于将多个可迭代对象打包成一个元组的迭代器。<br>\n",
    "例题2：我跟科比合砍81分！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "681d6a61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Me 0\n",
      "Kobe 81\n"
     ]
    }
   ],
   "source": [
    "player = [\"Me\",\"Kobe\"]\n",
    "score = ['0', '81']\n",
    "zipped = zip(player, score)\n",
    "zipped_list = list(zipped)  \n",
    "for player, score in zip(player, score):\n",
    "    print(player, score) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "862907a5",
   "metadata": {},
   "source": [
    "来看一些特殊情况\n",
    "1. 多个可迭代对象打包成元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5556ef55",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'x', 10.1), (2, 'y', 20.2), (3, 'z', 30.3)]\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "b = ['x', 'y', 'z']\n",
    "c = [10.1, 20.2, 30.3]\n",
    "\n",
    "result = list(zip(a, b, c))\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29457ad5",
   "metadata": {},
   "source": [
    "2.长度不一致的情况\n",
    "zip() 会在最短的可迭代对象耗尽时停止。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "91427275",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (2, 'b')]\n"
     ]
    }
   ],
   "source": [
    "list1 = [1, 2, 3, 4]\n",
    "list2 = ['a', 'b']\n",
    "\n",
    "print(list(zip(list1, list2)))\n",
    "# 注意：3 和 4 被忽略"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7592ffa3",
   "metadata": {},
   "source": [
    "3.  如果只传入一个可迭代对象，zip() 会将其每个元素包装成单元素元组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fbd82ef3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1,), (2,), (3,)]\n"
     ]
    }
   ],
   "source": [
    "print(list(zip([1, 2, 3])))  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91e7919e",
   "metadata": {},
   "source": [
    "### lambda函数\n",
    "lambda函数，也称为匿名函数，是一种简洁的定义小型函数的方式(不要写的太复杂！)。<br>\n",
    "lambda 函数是 Python 中一种创建匿名函数的简洁语法，常用于需要一个简单函数但又不想正式定义（用 def）的场景，尤其在配合 map()、filter()、sorted()、apply() 等函数式编程工具时非常高效。\n",
    "```python\n",
    "lambda 参数: 表达式\n",
    "```\n",
    "基本语法\n",
    "- 参数：函数的输入，可以有多个参数，用逗号分隔。\n",
    "- 表达式：函数的返回值，必须是单个表达式，不能包含复杂的语句。\n",
    "\n",
    "基本用法\n",
    "```python\n",
    "add = lambda x, y: x + y\n",
    "result = add(2, 3)  # 输出: 5\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94b976f7",
   "metadata": {},
   "source": [
    "### map() \n",
    "是一个内置函数，用于将一个函数应用到一个或多个可迭代对象的每个元素上，并返回一个 map 对象（迭代器）。\n",
    "```python\n",
    "map(function, iterable, ...)\n",
    "```\n",
    "- `function`：要应用的函数。\n",
    "- `iterable`：一个或多个可迭代对象，如列表、元组等。\n",
    "- 返回值：返回一个 map 对象(惰性求值的迭代器)，可以通过 `list()` 或 `for` 循环将其转换为列表或其他可迭代形式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08d99669",
   "metadata": {},
   "outputs": [],
   "source": [
    "def square(x):\n",
    "    return x * x\n",
    "\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "squared2 = map(square, numbers)\n",
    "for num in squared2:\n",
    "    print(num)\n",
    "#只能使用一次\n",
    "print(list(squared2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbf9ce68-73ab-44da-8732-f8b49df066b8",
   "metadata": {},
   "source": [
    "1. 结合lambda函数，做一下对比。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2e329a14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25]\n"
     ]
    }
   ],
   "source": [
    "numbers = [1, 2, 3, 4, 5]\n",
    "squared3 = map(lambda x: x * x, numbers)\n",
    "print(list(squared3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab6e2367",
   "metadata": {},
   "source": [
    "2. 与内置函数结合使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9136fabd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "str_nums = ['1', '2', '3', '4']\n",
    "int_nums = map(int, str_nums)\n",
    "print(list(int_nums))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "691609ca-6349-4845-b223-81b3fd19e0c3",
   "metadata": {},
   "source": [
    "### sorted()\n",
    "排序\n",
    "注意常见有两种写法，\n",
    "1. 列表的 list.sort() 方法（原地排序，修改原列表）\n",
    "2. 内置函数 sorted()（返回新列表，不修改原数据）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "34411f0a-e5b3-4c4a-bdad-5c50273d8026",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 3, 4, 5]\n",
      "[3, 1, 4]\n",
      "[1, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 原始数据\n",
    "nums = [3, 1, 4, 1, 5]\n",
    "\n",
    "# 使用 list.sort() —— 修改原列表\n",
    "nums.sort()\n",
    "print(nums)        # [1, 1, 3, 4, 5]\n",
    "\n",
    "# 使用 sorted() —— 返回新列表\n",
    "original = [3, 1, 4]\n",
    "new_list = sorted(original)\n",
    "print(original)    # [3, 1, 4] （不变）\n",
    "print(new_list)    # [1, 3, 4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07e08f81-a032-4290-b8c1-d4f6d70892b2",
   "metadata": {},
   "source": [
    "sort()中有两个关键参数。\n",
    "- 控制排序顺序：reverse 参数\n",
    "- 自定义排序规则：key 参数（数据分析中非常常用）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "762ba52c-c44a-42b0-9347-a447308b5155",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 3, 4, 5]\n",
      "[5, 4, 3, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "data = [3, 1, 4, 1, 5]\n",
    "# 升序（默认）\n",
    "print(sorted(data)) # [1, 1, 3, 4, 5]\n",
    "# 降序\n",
    "print(sorted(data, reverse=True)) # [5, 4, 3, 1, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce100892-22dc-427c-891d-9679628a8f3a",
   "metadata": {},
   "source": [
    "例子3: 将下列这句话中的每个单词按他们的长度顺序(从小到大)排列<br>\n",
    "\"Data analysis with Python is fun\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1a802f1d-cf57-4ec5-a117-7655c98e7277",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['is', 'fun', 'Data', 'with', 'Python', 'analysis']\n"
     ]
    }
   ],
   "source": [
    "sentences = \"Data analysis with Python is fun\"\n",
    "words = sentences.split(\" \")\n",
    "print(sorted(words, key=len))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7de62989-e109-439d-9013-b8f2c2f9db1c",
   "metadata": {},
   "source": [
    "1. 那如果我们的数据是一个二维的怎么办？\n",
    "比如:统计一下这场比赛的表现，按得分多少排序\n",
    "```python\n",
    "player=[\"Me\",\"Kobe\",\"Yao\",\"O'Neal\"]\n",
    "scores=[0,81,41,40]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5c0c3e8a-6f4c-4e85-b7d2-759107a24a29",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Performance: [('Me', 0), ('Kobe', 81), ('Yao', 40), (\"O'Neal\", 40)]\n",
      "Sorted by score (desc): [('Kobe', 81), ('Yao', 40), (\"O'Neal\", 40), ('Me', 0)]\n"
     ]
    }
   ],
   "source": [
    "player=[\"Me\",\"Kobe\",\"Yao\",\"O'Neal\"]\n",
    "scores=[0,81,41,40]\n",
    "performance = list(zip(player, scores))\n",
    "print(\"Performance:\", performance)\n",
    "\n",
    "sorted_performance = sorted(performance, key=lambda item: item[1], reverse=True)\n",
    "print(\"Sorted by score (desc):\", sorted_performance)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4374965d-c078-46fa-b838-d7c731bc35c5",
   "metadata": {},
   "source": [
    "2. 多级排序（多重条件）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "56ed9500-bf3d-410c-a749-27b2b36a2116",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sorted by score fisrt,then height: [('Kobe', 81, 198), ('Yao', 41, 225), (\"O'Neal\", 40, 218), ('Me', 0, 140)]\n"
     ]
    }
   ],
   "source": [
    "player=[\"Me\",\"Kobe\",\"Yao\",\"O'Neal\"]\n",
    "scores=[0,81,41,40]\n",
    "height =[140,198,225,218]\n",
    "performance = list(zip(player, scores,height))\n",
    "sorted_performance = sorted(performance, key=lambda item: (item[1],item[2]), reverse=True)\n",
    "print(\"Sorted by score fisrt,then height:\", sorted_performance)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b41b5418-b13c-42b9-91cb-568fcd3cb57e",
   "metadata": {},
   "source": [
    "### 常见误区\n",
    "在 lambda 中写语句\n",
    "```python\n",
    "f = lambda x: y = x*2   # 语法错误！\n",
    "```\n",
    "\n",
    "不推荐：太复杂\n",
    "```python\n",
    "result = sorted(data, key=lambda x: (lambda y: y[1] if y[0] > 0 else -y[1])(x)) \n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cefb776",
   "metadata": {},
   "source": [
    "### filter() \n",
    "用来筛选序列中符合条件的元素。\n",
    "```python \n",
    "filter(function, iterable)\n",
    "```\n",
    "- `function`：用于测试每个元素的函数，返回 True 或 False。\n",
    "- `iterable`：一个可迭代对象，如列表、元组等。\n",
    "- 返回值：返回一个 filter 对象（迭代器），可以通过 `list()` 或 `for` 循环将其转换为列表或其他可迭代形式。\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b573d77",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c7d8a290",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "numbers = [1, 2, 3, 4, 5, 6]\n",
    "\n",
    "def is_even(n):\n",
    "    return n % 2 == 0\n",
    "\n",
    "result = filter(is_even, numbers)\n",
    "print(list(result))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2a1f7e7",
   "metadata": {},
   "source": [
    "用 lambda 简化写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e32e8d43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "numbers = [1, 2, 3, 4, 5, 6]\n",
    "result = filter(lambda x: x % 2 == 0, numbers)\n",
    "print(list(result))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef36f144",
   "metadata": {},
   "source": [
    "过滤空字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c9fc5042",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'AI', 'Data']\n"
     ]
    }
   ],
   "source": [
    "words = [\"Python\", \"\", \"AI\", \"\", \"Data\"]\n",
    "filtered = list(filter(None, words))  # None 表示直接过滤 False 值\n",
    "print(filtered)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "211e1d2a-c230-4978-86aa-250f27d93d46",
   "metadata": {},
   "source": [
    "### enumerate()\n",
    "`enumerate()` 是 Python 的一个内置函数，用于将一个可迭代对象（如列表、元组或字符串）组合为一个索引序列，同时列出数据和数据下标，常用于在循环中获取元素的索引和值。\n",
    "```pythonenumerate(iterable, start=0)\n",
    "```\n",
    "- `iterable`：一个可迭代对象，如列表、元组、字符串等。\n",
    "- `start`：用于指定索引的起始值，默认为0。      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1200226e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 apple\n",
      "1 banana\n",
      "2 cherry\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "for index, fruit in enumerate(fruits):\n",
    "    print(index, fruit)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d28e69a",
   "metadata": {},
   "source": [
    "### all() \n",
    "用于判断 可迭代对象中的所有元素是否都为真值（True）。\n",
    "```python\n",
    "all(iterable)\n",
    "```\n",
    "- `iterable`：一个可迭代对象，如列表、元组等。\n",
    "- 返回值：如果可迭代对象中的所有元素都为真值，返回 True；否则返回 False。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb45f8a5",
   "metadata": {},
   "source": [
    "更多python内置函数，可以参考官方文档：https://docs.python.org/3/library/functions.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "161aac34-7d68-4c0c-95e0-326a2bfa8999",
   "metadata": {},
   "source": [
    "### index()\n",
    "用来查找某个元素在序列（如字符串、列表或元组）中第一次出现的位置（索引）。\n",
    "```python\n",
    "sequence.index(x[, start[, end]])\n",
    "```\n",
    "参数说明：\n",
    "- x：要查找的元素。\n",
    "- start（可选）：从哪个位置开始查找（包含该位置）。\n",
    "- end（可选）：查找到哪个位置为止（不包含该位置）。\n",
    "\n",
    "返回值：\n",
    "- 返回元素第一次出现的索引（从 0 开始）。\n",
    "- 如果找不到，会抛出 ValueError 异常。\n",
    "```python    \n",
    "fruits = ['apple', 'banana', 'cherry', 'banana']\n",
    "print(fruits.index('banana'))       # 输出 1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7db4c902",
   "metadata": {},
   "source": [
    "### 练习题\n",
    "每个月的销售额\n",
    "```python\n",
    "sales = [1200, 1500, 1800, 1600, 2100]\n",
    "months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\"]\n",
    "```\n",
    "1. 计算每月环比增长率\n",
    "2. 找出增长最快的月份\n",
    "3. 判断销售是否持续增长\n",
    "4. 将上述信息打印出来"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
